In [1]:
import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline

Regression

Load the boston dataset:


In [ ]:
from sklearn.datasets import load_boston
boston = load_boston()
boston.keys()

In [ ]:
print(boston.DESCR)

In [ ]:
boston.data.shape

In [ ]:
boston.target.shape

In [ ]:
boston.target

In [ ]:
from sklearn.cross_validation import train_test_split
X_train, X_test, y_train, y_test = train_test_split(boston.data, boston.target)

Learning a Regressor


In [ ]:
from sklearn.linear_model import Ridge

In [ ]:
ridge = Ridge()

In [ ]:
ridge.fit(X_train, y_train)

In [ ]:
pred_test = ridge.predict(X_test)
pred_test

R2 score:


In [ ]:
ridge.score(X_test, y_test)

MSE:


In [ ]:
from sklearn.metrics import mean_squared_error
mean_squared_error(y_test, pred_test)

Random Forest Regression


In [ ]:
from sklearn.ensemble import RandomForestRegressor

In [ ]:
rf = RandomForestRegressor()

In [ ]:
rf.fit(X_train, y_train)

In [ ]:
rf.score(X_test, y_test)

In [ ]:
mean_squared_error(y_test, rf.predict(X_test))